﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hockey.Shared;
using Microsoft.Practices.Prism.Events;
using Microsoft.Win32;
using System.IO;
using System.Windows;
using MVVMDialogs;
using Hockey.UILayer.Forms;
using Microsoft.Practices.Unity;
//using Gat.Controls;

namespace Hockey.UILayer.ViewModels
{
    /// <summary>
    /// ViewModel for the main menu
    /// </summary>
    public class HockeyMenuVM
    {
        private IBoard _board;
        private IEventAggregator _events;
        private IDialogService _dlgService;
        private IUnityContainer _unity;


        /// <summary>
        /// Constructor
        /// </summary>
        public HockeyMenuVM(IBoard board, IEventAggregator events, IUnityContainer unity, IDialogService dlgService)
        {
            if (board == null)
                throw new ArgumentNullException("board");
            _board = board;

            if (events == null)
                throw new ArgumentNullException("events");
            _events = events;

            if (unity == null)
                throw new ArgumentNullException("Unity");
            _unity = unity;

            if (dlgService == null)
                throw new ArgumentNullException("Dialog Service");
            _dlgService = dlgService;

            // create commands
            NewGameCommand = new AutoDelegateCommand(() => { NewGame(); });
            LoadGameCommand = new AutoDelegateCommand(
                () => { LoadGame(); },
                () => { return _board.IsMoveCompleted; }
            );
            SaveGameCommand = new AutoDelegateCommand(
                () => { SaveGame(); },
                () => { return _board.IsMoveCompleted; }
            );
            HelpCommand = new AutoDelegateCommand(() => { ShowHelp(); });
            LicenseCommand = new AutoDelegateCommand(() => { ShowLicenses(); });
            ExitAppCommand = new AutoDelegateCommand(() => { ExitApp(); });
        }

        /// <summary>
        /// Commands
        /// </summary>

        public AutoDelegateCommand NewGameCommand
        { get; set; }

        public AutoDelegateCommand LoadGameCommand
        { get; set; }

        public AutoDelegateCommand SaveGameCommand
        { get; set; }

        public AutoDelegateCommand HelpCommand
        { get; set; }

        public AutoDelegateCommand LicenseCommand
        { get; set; }

        public AutoDelegateCommand ExitAppCommand
        { get; set; }

        /// <summary>
        /// Starts the new game
        /// </summary>
        private void NewGame()
        {
            // run the form to setup the new game
            NewGameFormVM vm = _unity.Resolve<NewGameFormVM>();
            _dlgService.ShowDialog<NewGameForm>(this, vm);
            if (vm.Success)
            {
                // send notification to start the new game
                _events.GetEvent<StartNewGame>().Publish(vm.GameSettings);
            }
        }

        /// <summary>
        /// Load the existing game from fiel
        /// </summary>
        private void LoadGame()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;
            dlg.DefaultExt = "hockey";
            dlg.Filter = "Hockey soubory (*.hockey)|*.hockey";

            // open file dialog
            //OpenDialogView dlg = new OpenDialogView();
            //OpenDialogViewModel vm = dlg.DataContext as OpenDialogViewModel;
            //vm.AddFileFilterExtension(".hockey");

            //vm.CancelText = "Storno";
            //vm.Caption = "Načíst hru ze souboru";
            //vm.DateText = "Datum";
            //vm.FileNameText = "Cesta souboru";
            //vm.FileFilterText = "Přípona";
            //vm.SaveText = "Otevřít";
            //vm.SizeText = "Velikost";
            //vm.TypeText = "Typ souboru";
            //vm.NameText = "Soubor";

            //vm.StartupLocation = WindowStartupLocation.CenterOwner;
            //vm.Owner = Application.Current.MainWindow;       

            if (dlg.ShowDialog() == true)
            {
                // load the game from file
                try
                {
                    using (FileStream stream = new FileStream(dlg.FileName, FileMode.Open))
                    {
                        _board.Load(stream);
                    }

                    _events.GetEvent<BoardChanged>().Publish(null);
                }
                catch
                {
                    MessageBox.Show("Soubor se nepodařilo načíst.", "Chyba");
                }
            }
        }

        /// <summary>
        /// Save the existing game
        /// </summary>
        private void SaveGame()
        {
            // show file save dialog
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.CheckPathExists = true;
            dlg.DefaultExt = "hockey";
            dlg.Filter = "Hockey soubory (*.hockey)|*.hockey";
            if (dlg.ShowDialog() == true)
            {
                try
                {
                    using (FileStream stream = new FileStream(dlg.FileName, FileMode.Create))
                    {
                        _board.Save(stream);
                    }
                }
                catch
                {
                    MessageBox.Show("Soubor se nepodařilo uložit.", "Chyba");
                }
            }
        }

        /// <summary>
        /// Show the help
        /// </summary>
        private void ShowHelp()
        {
            HelpFormVM vm = _unity.Resolve<HelpFormVM>();
            _dlgService.ShowDialog<HelpForm>(this, vm);
        }

        /// <summary>
        /// Method for showing the License Dialog
        /// </summary>
        private void ShowLicenses()
        {
            LicensesVM vm = _unity.Resolve<LicensesVM>();
            _dlgService.ShowDialog<Licenses>(this, vm);
        }

        /// <summary>
        /// Called to send the notification to exit the app
        /// </summary>
        private void ExitApp()
        {
            _events.GetEvent<ExitApplication>().Publish(null);
        }
    }
}
